﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;
using System.Text;

namespace Interface.Rooms
{
    public partial class RoomList : System.Web.UI.Page
    {
        DataConnection dc = new DataConnection();
        DataValid dv = new DataValid();
        string stringDetail;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["AdminSession"] != null || Session["OwnerSession"] != null)
            {
                btnAdd.Visible = true;           
            }
            
            getQueryString();
        }

        //protected DataTable dataTableInitiate()
        //{
        //    DataTable dt = new DataTable();
        //    DataColumn col = dt.Columns.Add("ID", typeof(int));
        //    col.AutoIncrement = true;
        //    col.AutoIncrementStep = 1;
        //    col.AutoIncrementSeed = 1;

        //    dt.Columns.Add("STT", typeof(int));
        //    dt.Columns.Add("TenPhong: ", typeof(string));
        //    dt.Columns.Add("DienTich: ", typeof(string));
        //    dt.Columns.Add("Gia: ", typeof(string));
        //    dt.Columns.Add("TinhTrang: ", typeof(string));

        //    return dt;
        //}

        private void getQueryString()
        {
            if (Request.QueryString["ID"] != null)
            {
                stringDetail = Request.QueryString["ID"].ToString();
                DetailBind(stringDetail);
            }
        }

        private void DetailBind(string houseId)
        {
            string sqlRoom = "Select * from Rooms where HouseID='" + houseId + "'";
            DataTable dt = dc.getData(sqlRoom);
            //DataTable dtHouse = dataTableInitiate();
            StringBuilder roomList = new StringBuilder();

            string sqlAddress = "Select * from BoardingHouse where IdHouse = '" + houseId + "'";
            DataTable dt1 = dc.getData(sqlAddress);

            string _address = null;
            if (dt1.Rows.Count > 0)
            {
                                    
                DataRow row = dt1.Rows[0];
                string _Street = row["Street"].ToString();
                string _Ward = row["Ward"].ToString();
                int _idDistrict = Convert.ToInt32(row["District"]);
                string strsql = "Select Name from District where  IdDistrict ='" + _idDistrict + "'";
                string _dis = dc.getStringValue(strsql);


                int _idCity = Convert.ToInt32(row["City"]);
                string strsql1 = "Select Name from City where  IdCity ='" + _idCity + "'";
                string _city = dc.getStringValue(strsql1);

                _address = "Địa chỉ: " + _Street + ", p. " + _Ward +", "+ _dis + ", tp. " + _city;

            }


            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int stt = i + 1;
                    DataRow row = dt.Rows[i];
                    

                    string _idRoom = row["RoomID"].ToString();
                    string _nameOfRoom = row["Name"].ToString();
                    string _area = row["Area"].ToString();
                    string _price = row["Price"].ToString();
                    int _available = Convert.ToInt32(row["Available"]);
                    string _tinhtrang = "Còn chổ";
                    if (_available == 1)
                    {
                        _tinhtrang = "Phòng đả đầy";

                    }

                    string imgRoom = row["ImageSource"].ToString();
                    string _roomDetail = "RoomDetails.aspx?ID=" + _idRoom;
                    string _imgRoom = "/Images/Room/" + imgRoom;

                    roomList.AppendFormat(@"<div class='RL-box-border'>
                                <div class='RL-title'><span class='maukhac'> {0} </span> </div>
                                <div class='RL-detail-link' ><a href={1} >Chi tiết</a></div>
                                <div class='RL-image'><img width='100' height='100' src={2}> </div>
                                <div class='RL-detail'>Tên Phòng: <spam class='maukhac' >{3} </spam> </div>
                                <div class='RL-detail'>Diện Tích: <spam class='maukhac'>{4}</spam> m2</div>
                                <div class='RL-detail'>Giá tiền: <spam class='maukhac'>{5}</spam></div>
                                <div class='RL-detail'>Tình Trạng: <spam class='maukhac'>{6}</spam></div>
                                    
                                
                                </div>", _address, _roomDetail, _imgRoom, _nameOfRoom, _area, _price, _tinhtrang);

                    lit.Text = roomList.ToString();

                    
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Rooms/AddNewRoom.aspx");
        }


    }
}
